<?php
session_start();
$_SESSION[$z_current_team_id.'_rpage'] = $_SERVER['REQUEST_URI']; 
if(!isset($_SESSION[sw_username])){header("location:../login/");exit;}
require ("../_sw_config/config.php"); 
if ($_SESSION[$z_current_team_id.'_admin'] != 't'){header("location:../login/?error=2"); exit;}


require ($rmkdir_global); //required for file uploads, directory creation
require ($cleanfile_global); //required for file uploads, cleans filename
require ($checkfile_global); //checks if file already exits, so no overwrite of files
require ($thumbnailer_global); 
 
if ($_SERVER['REQUEST_METHOD'] == "POST") { 

$title		= pg_escape_string($_POST['name']);

//******************  defaults Select Query  ***************
$query_defaults = "SELECT * FROM site_defaults WHERE team_id='$z_current_team_id'";
$result_defaults = pg_query($connection, $query_defaults) or die("Error in query: $query_defaults.". pg_last_error($connection));
$rows_defaults = pg_num_rows($result_defaults);
//********************************************************
$arr = pg_fetch_array($result_defaults, NULL, PGSQL_ASSOC);
$site_thumb_size = $arr['thumb_size'];



//LOGO UPLOAD
if (($_FILES['image1']['name']) != ""){

$current_upload_file_loc = $_FILES['image1']['tmp_name'];
$current_upload_file_name = basename($_FILES['image1']['name']);
$upload_folder = "banners/";
require ($s3_upload_global); //required for file uploads, directory creation
$file = $s3_upload_output;


//Create Thumbnail
$tn_name =  $file_part1.$timestamp."_tn.".$file_part2;
$temp_loc = $global_upload_dir.$upload_folder;
rmkdir($temp_loc);
$thumbnail = global_resize($file, $site_thumb_size, $tn_name, $temp_loc);
$current_upload_file_loc = $thumbnail;
$current_upload_file_name = $tn_name;
require ($s3_upload_global); //required for file uploads, directory creation
$thumbnail = $s3_upload_output;
unlink($current_upload_file_loc);
}



//**************** INSERT INTO DATABASE (Meet TABLE) ********************************
$query = "INSERT INTO banners (banner_title, banner_image, banner_thumb, banner_active, team_id)".
       " VALUES ('$title', '$file', '$thumbnail', 'TRUE', '$z_current_team_id')";
pg_query($connection, $query) or die("Error in query: $query.". pg_last_error($connection)); 
//**********************************************************************



//***********************************
//Add to Admin Actions list
$action		= "Added $title to Banners";
require ($action_global); //required for file uploads, directory creation

header("Location:banner_list.php");exit;

}//END POST 
//*************************
 
?>

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" type="text/css" href="<?php echo $admin_css;?>"/>
<title><?php echo $global_title;?> Banner Add</title>

<script language="javascript" type="text/javascript">
    var submitcount = 0;
    function verify()
    {
      // Check for empty fields
      
      if(document.form1.image1.value == "")
      {
        alert("Please Add an Image");
        document.form1.image1.focus();
        return false;
      }
      
	        
      if(submitcount == 0)
      {
        submitcount++;
        return true;
      }
      else
      {
        alert("This form has already been submitted.  Thanks!");
        return false;
      }
    }
</SCRIPT>
</head>
<body>

<div id="wrap">
	<div id="page-header">
		<?php include('includes/header.php');?>

	</div>
	
	<div id="page-body">
		<div id="tabs">
			<?php $tab="addons"; include('includes/header_tabs.php');?>
		</div>

		<div id="acp">
		<div class="panel">
			<span class="corners-top"><span></span></span>
				<div id="content">
				<div id="menu">
						
					<?php include('includes/menu_top.php');?>	
					<?php include('includes/menu_addons.php');?>
					</div>
	
					<div id="main">
<a name="maincontent"></a>


	<h1>Add Banner</h1>

<form id="form1" name="form1" method="post" action="" enctype="multipart/form-data" >

		<fieldset><legend>Add New Banner</legend>

          <dl>
                <dt>Banner Title:</dt>
                <dd><input type="text" name="name" id="name" autofocus required size="50" /></dd>
         </dl>
                
           <dl>
                <dt>Image File (JPG, PNG, GIF):</dt>
                <dd><input type="file" name="image1" id="image1" required></dd>
           </dl> 
         
      
      <p class="submit-buttons"><input type="submit" class="button1" name="submit" id="submit" value="Submit" /></p>
   	</fieldset>
    </form>
    
		</div><!-- main -->
	</div><!-- content -->
<span class="corners-bottom"><span></span></span>
</div><!-- panel -->
</div><!-- acp -->

	
	<div id="page-footer">
	<?php include('includes/footer.php');?>
				</div>
</div><!-- page-body -->
</div><!-- wrap -->


</body> 
</html>